package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RequestMapping("/smallVideos")
@RestController
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;


    /*
     *  小视频上传
     *    请求方式和请求链接 : POST
     *    请求参数:Body  videoThumbnail-视频封面文件 ，videoFile-视频文件
     *    返回值: 无
     * */
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile ) throws IOException {

        smallVideosService.saveVideo(videoThumbnail,videoFile);

        return ResponseEntity.ok(null);
    }

    /*
     *  小视频列表
     *    请求方式和请求链接 : GET
     *    请求参数: Query  page,pagesize
     *    返回值:PageResult<VideoVo>
     * */

    @GetMapping
    public ResponseEntity smallVideos(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "1") Integer pagesize){

        PageResult result =smallVideosService.findVideos(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /*
     *  视频用户关注
     *    请求方式和请求链接 : POST   /{focusId}/userFocus
     *    请求参数:focusId(视频主人的userId)
     *    返回值: 无
     * */
     @PostMapping("/{focusId}/userFocus")
    public ResponseEntity userFocus(@PathVariable("focusId")  Long focusId ){

         smallVideosService.userFocus(focusId);
         return ResponseEntity.ok(null);
     }

    /*
     *  视频用户关注 - 取消
     *    请求方式和请求链接 :POST  /:uid/userUnFocus
     *    请求参数:  focusId(视频主人的userId)
     *    返回值: 无
     * */

    @PostMapping("/{focusId}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("focusId")  Long focusId ){

        smallVideosService.userUnFocus(focusId);
        return ResponseEntity.ok(null);
    }
    /**
     * 视频点赞
     *  接口路径：POST       /:id/like
     *  路径参数：id     视频id
     */
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable("id") String id){
        smallVideosService.videoLike(id);
        return ResponseEntity.ok(null);
    }
    /**
     * 视频点赞 - 取消
     *  接口路径：POST       /:id/dislike
     *  路径参数：id     视频id
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDisLike(@PathVariable("id") String id ){
        smallVideosService.videoDisLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论列表
     *  接口路径：GET    /:id/comments
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable("id") String id ){
        PageResult result=smallVideosService.findAllComments(id,page,pagesize);

        return ResponseEntity.ok(result);
    }
    /**
     * 评论发布
     *  接口路径：POST   /:id/comments
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") String id ,
                                       @RequestBody Map map){
        String comment = (String) map.get("comment");

        smallVideosService.saveComments(id,comment);
        return ResponseEntity.ok(null);
    }
    /**
     * 评论点赞
     *  接口路径：POST       /comments/:id/like
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String id){
        ObjectId commentsId = new ObjectId(id);
        smallVideosService.commentsLike(commentsId);
        return ResponseEntity.ok(null);
    }
    /**
     * 评论点赞 - 取消
     *  接口路径：POST   /comments/:id/dislike
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDisLike(@PathVariable("id") String id){
        ObjectId commentsId = new ObjectId(id);
        smallVideosService.commentsDisLike(commentsId);
        return ResponseEntity.ok(null);
    }
}
